<?php

namespace app\common\model\xilumarket;

use think\Model;
use function fast\array_get;


class ProductCategory extends Model
{

    

    

    // 表名
    protected $name = 'xilumarket_product_category';

    // 追加属性
    protected $append = [
        'status_text',
        'thumb_image_text'
    ];

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getTopStatusList()
    {
        return ['0' => __('Top_status 0'), '1' => __('Top_status 1')];
    }

    public function getThumbImageTextAttr($value,$data){
        return !empty($data['thumb_image']) ? cdnurl($data['thumb_image'],true) : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function pcate(){
        return $this->belongsTo(self::class,'pid','id',[],'inner')->setEagerlyType(1);
    }

    public function childlist(){
        return $this->hasMany(self::class,'pid','id');
    }

    /**
     * 推荐分类
     * @param int $pagesize
     */
    public static function recommendLists($pagesize = 4){
        $product_model = new Product();
        $lists = self::field('path,name_path,weigh,createtime,updatetime',true)
            ->where('status',"1")
            ->where('pid',0)
            ->where('recommend_status','1')
            ->order('weigh','asc')
            ->limit($pagesize)
            ->select();
        foreach ($lists as $list){
            $ids = self::where('pid',$list->id)->column("id");
            $list->product_count = $ids ? $product_model->where('product_type',1)->where("status",2)->where("check_status",2)->whereIn('category_id',$ids)->count(1) :0;
        }
        return $lists;
    }

    /**
     * 推荐分类
     * @param int $pagesize
     */
    public static function recommendSecondLists($pagesize = 4){
        $lists = self::alias('cate')
            ->join("xilumarket_product_category pcate",'pcate.id=cate.pid')
            ->field('cate.id,cate.name')
            ->where('pcate.status',"1")
            ->where('cate.status',"1")
            ->where('cate.pid','>',0)
            ->where('cate.recommend_status',"1")
            ->order('cate.weigh','asc')
            ->limit($pagesize)
            ->select();
            // var_dump($lists->toArray());
            if($lists->isEmpty()){
                return [['id'=>-1,'name'=>'猜你喜欢']];
            }
        return array_merge([['id'=>-1,'name'=>'猜你喜欢']],$lists->toArray());
    }


    /**
     * 话题列表
     * @param $params
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public static function lists($params){
        $where['status'] = 1;
        $where['pid'] = 0;
        //搜索
        return self::with(['childlist'=>function($query){
//            $query->field('path,name_path,weigh,createtime,updatetime',true);
            $query->field('id,name,pid,thumb_image');
        }])
//            ->field('path,name_path,weigh,createtime,updatetime',true)
            ->field('id,name,thumb_image')
            ->where($where)
            ->order('updatetime','desc')
            ->select();
//            ->each(function ($row) use($product_model){
//                $row->product_count = $product_model->where('category_id',$row->id)->count("id");
//            });
    }


}
